﻿// 订单配送

// 解析URL
var parameter = window.location.href.split("?")[1];
var parameterList = parameter.split("&");
var order_id = decodeURIComponent(parameterList[0]);  //订单编号
var type = decodeURIComponent(parameterList[1]);  //跳转类型（1：上个页面为到店自取；2：上个页面为在线配送）
var orderState = decodeURIComponent(parameterList[2]);  //订单状态
var username = decodeURIComponent(parameterList[3]);  //用户名
var hospital = decodeURIComponent(parameterList[4]);  //医院
var source = decodeURIComponent(parameterList[5]);  //来源
var department = decodeURIComponent(parameterList[6]);  //科室
var state = decodeURIComponent(parameterList[7]);  //状态
var searchword = decodeURIComponent(parameterList[8]);  //查询词
var timefrom = decodeURIComponent(parameterList[9]);  //开始时间
var timeto = decodeURIComponent(parameterList[10]);  //结束时间
var pageNo  = decodeURIComponent(parameterList[11]);  //页码
var pageSize = decodeURIComponent(parameterList[12]);  //每页显示数据数目
var express_price;
var flag;
var express_code;
console.log(parameterList);

//通过订单编号查询订单信息
$.ajax({
    type: "post",
    url: "/ysfwpt/selectOrderDetailByOrderId",
    async :false,
    data: {
        order_id: order_id
    },
    success: function (data) {
        // console.log("success");
        console.log(data);
        $("#order_id").text(data.order.order_id);
        $("#patient_name").text(data.order.patient_name);
        $("#treatment_card").text(data.order.treatment_card);
        $("#patient_phone").text(data.order.patient_phone);
        $("#hospital").text(data.order.hospital);

        $("#receive_person_name").text(data.order.receive_person_name);
        $("#receive_person_phone").text(data.order.receive_person_phone);
        $("#receive_person_post_code").text(data.order.receive_person_post_code);
        $("#receive_person_addr").text(data.order.receive_person_addr);

        express_price = data.order.express_price;
        $("#process_img").attr("src","/pic/pic_process8.png");

        //判断是否为处方单
        if(data.order.prescription_id == null || data.order.prescription_id == ""){
            flag = 0;
        }
        else{
            flag = 1;
        }
    },
    error: function (data) {
        // console.log("error");
        // console.log(data);
    }
});

//通过订单编号查询药品信息
$.ajax({
    type: "post",
    url: "/ysfwpt/selectMedicineByOrderId",
    data: {
        order_id: order_id
    },
    success: function (data) {
        // console.log("success");
        // console.log(data);
        var medicineList = data.list;
        for(var i=0;i<data.list.length;i++) {
            var no = parseInt(i)+1;
            var medicineinfo = medicineList[i];
            var strhtml = '<tr id="' + i + '" arrId="' + i + '">';
            strhtml = strhtml + '<td>' + no + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_drug_name + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_drug_capacity + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_drug_manufacturer + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_drug_price + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_drug_amount + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_drug_unit + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_total_price + '</td>';
            if(i==0){
                if(flag == 1){
                    strhtml = strhtml + '<td rowspan=' + data.list.length + ' style="color:#5AA0DC">查看</td>';
                }
                else{
                    strhtml = strhtml + '<td rowspan=' + data.list.length + ' style="color:#5AA0DC">无</td>';
                }
                strhtml = strhtml + '<td rowspan=' + data.list.length + ' style="color:#F6B751">' + data.total + '</td>';
            }
            $("#medicinedetail_list").append(strhtml);
        }
        $("#drue_price").text(data.total + "元");
        $("#delivery_price").text(express_price + "元");
        $("#total_price").text(data.total + express_price + "元");
    },
    error: function (data) {
        // console.log("error");
        // console.log(data);
    }
});

// 搜索框下拉数据
$.ajax({
    type: "get",
    url: "/ysfwpt/selectPleasant",
    async: false,
    data: {},
    success: function(data) {
        // console.log("success");
        // console.log(data);
        for(var i = 0;i < data.list.length;i ++){
            $("#express_company_name_list ul").append("<li id=" + data.list[i].code + " class='list'>" + data.list[i].name + "</li>");
        }
    }
});

// 设置下拉框宽度（输入框id1，下拉框id2）
var setWidth = function (id1,id2) {
    var width = $("#" + id1).css('width');
    $("#" + id2).css({
        "width": width
    });
};
// 下拉框定位函数（输入框id，下拉列表id）
var num = 0;
var time;
var resetList = function (id1,id2) {
    var top = $("#" + id1).offset().top;
    var left = $("#" + id1).offset().left;
    var width = $("#" + id1).css('width');
    var hide = $(window).scrollTop();
    $("#" + id2).css({
        "top": top - hide + 30,
        "left": left,
        "width": width
    });
    num++;
    console.log(num);
};
var stopList = function () {
    num = 0;
    clearInterval(time);
};
// 点击搜索框
$("#express_company_name,.icon").click(function (e){
    // e.stopPropagation(); //阻止冒泡
    // 显示时隐藏
    if($("#express_company_name_list").is(':visible')){
        // stopList();
        $("#express_company_name_list").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
    }
    // 隐藏时显示
    else{
        // 隐藏所有列表
        // stopList();
        $(".popup_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        // 将下拉框位置定位到点击的输入框下方
        // time = setInterval(function () {
            resetList("express_company_name","express_company_name_list");
        // },0);
        // 显示当前列表
        setWidth("express_company_name","express_company_name_list div");
        $("#express_company_name_list").slideDown(300);
        $(".icon").css("transform","rotate(180deg)");
        // 获取点击的内容并显示在输入框内
        var obj_lis = $("#express_company_name_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                // stopList();
                express_code = this.id;
                $("#express_company_name").val(this.innerHTML);
                $("#express_company_name_list").slideUp(300);
                $("#express_company_name").parent().find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});

//确认发货
$("#ship").click(function () {
    $('#ship_popup').show();
    $(".shandow").show().css('z-index', '11');
    $('body').css({
        "overflow-x":"hidden",
        "overflow-y":"hidden"
    });
});
//关闭确认发货弹窗
$("#ship_popup_top div,#ship_popup_bottom button:nth-child(2)").bind("click", function() {
    $("#ship_popup").hide();
    $(".shandow").hide().css('z-index','-1');
    $('body').css({
        "overflow-x":"auto",
        "overflow-y":"auto"
    });
    $("#express_company_name_list").slideUp(300);
    $(".icon").css("transform","rotate(0deg)");
});
//确定提交确认发货申请
$("#ship_popup_bottom button:nth-child(1)").bind("click", function() {
    var express_company_name = $("#express_company_name").val();
    var express_price = $("#express_price").val();
    var express_id = $("#express_id").val();
    if(express_company_name == "" || express_price == "" || express_id == ""){
        alert("请输入快递信息！");
    }
    else{
        $.ajax({
            type: "post",
            url: "/ysfwpt/updateOrderExpress",
            data: {
                order_id: order_id,
                express_company_name: express_code,
                express_price: express_price,
                express_id: express_id
            },
            success: function (data) {
                // console.log("success");
                // console.log(data);
                //再修改该订单状态
                $.ajax({
                    type: "post",
                    url: "/ysfwpt/updateOrderState",
                    data: {
                        order_id: order_id,
                        state: "4",
                        reason_name: "",
                        reason: ""
                    },
                    success: function (data) {
                        // console.log("success");
                        // console.log(data);
                        back();
                    },
                    error: function (data) {
                        // console.log("error");
                        // console.log(data);
                    }
                });
            },
            error: function (data) {
                // console.log("error");
                // console.log(data);
            }
        });
    }
});

//返回上个页面
$("#label div:nth-child(2)").click(function () {
    back();
});

var back = function (name) {
    var name;
    if(type == 1){
        name = "ys_service_offline";
    }
    if(type == 2){
        name = "ys_service_online";
    }
    window.parent.document.getElementById("iframe_right").src = "/ysfwpt/" + name + "?" + username + "&" + hospital + "&" + source + "&" + department + "&" + state + "&" + searchword + "&" + timefrom + "&" + timeto + "&" + pageNo + "&" + pageSize;
};